package com.yanqu.road.dao.impl.union.cross.huaben;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.huaben.rank.HuaBenWeekServerGroup;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHuaBenWeekServerGroupDaoImpl extends TempDataObjectDao<HuaBenWeekServerGroup> {

    public static String getHuaBenWeekServerGroupSql = "select * from `t_c_hua_ben_week_server_group` where `weekId`=? or `weekId`=?";

    public Map<Integer, Map<Integer, HuaBenWeekServerGroup>> getHuaBenWeekServerGroup(int preWeekId, int thisWeekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, HuaBenWeekServerGroup>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHuaBenWeekServerGroupSql);
            int i = 1;
            call.setInt(i++, preWeekId);
            call.setInt(i++, thisWeekId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaBenWeekServerGroup data = parse(rs);
                if(!result.containsKey(data.getWeekId())){
                    result.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                result.get(data.getWeekId()).put(data.getGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertHuaBenWeekServerGroupSql = "insert into `t_c_hua_ben_week_server_group`(weekId,serverList,groupId) values (?,?,?)";

    @Override
    protected boolean addData(HuaBenWeekServerGroup data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertHuaBenWeekServerGroupSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateHuaBenWeekServerGroupSql = "update `t_c_hua_ben_week_server_group` set serverList=? where weekId=? and groupId=?";

    @Override
    protected boolean updateData(HuaBenWeekServerGroup data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateHuaBenWeekServerGroupSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected HuaBenWeekServerGroup parseData(ResultSet rs) throws SQLException {
        HuaBenWeekServerGroup data = new HuaBenWeekServerGroup();
        data.setWeekId(rs.getInt("weekId"));
        data.setServerList(JSON.parseObject(rs.getString("serverList"), new TypeReference<List<Long>>() {}));
        data.setGroupId(rs.getInt("groupId"));

        return data;
    }
}
